﻿//	Map_FVG C# Class Library.
//
//	infoDialog7
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class infoDialog7 : Form
    {
        public infoDialog7()
        {
            InitializeComponent();

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select * from testo WHERE codice_testo = '";
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");

                    //          MessageBox.Show(ss);
                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            textBox1.Text = reader.GetString(0).ToString();
                            textBox2.Text = reader.GetString(1).ToString();
                            textBox3.Text = reader.GetString(2).ToString();
                            textBox4.Text = reader.GetString(3).ToString();
                            textBox7.Text = reader.GetString(4).ToString();
                        }
                    }
                }

                conn.Close();
            }

            string s1 = textBox3.Text;

            Font ifont = stringToFont(s1);
            textBox5.Text = ifont.Size.ToString();
            textBox6.Text = ifont.Height.ToString();

            string[] words;
            char[] delimiterChars = { ' ', ',' };

            s1 = textBox4.Text;
            textBox4.Text = "";
            words = s1.Split(delimiterChars);
            byte b1 = byte.Parse(words[0]);
            byte b2 = byte.Parse(words[1]);
            byte b3 = byte.Parse(words[2]);
            byte b4 = byte.Parse(words[3]);
            textBox4.BackColor = Color.FromArgb(b1, b2, b3, b4);

        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  conferma
            string s2 = textBox2.Text;
            if (s2.Length == 0)
            {
                MessageBox.Show("manca denominazione proprietà");
                return;
            }
            string s3 = textBox3.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("indicare font");
                return;
            }

            Color icol = textBox4.BackColor;

            string ss = icol.A.ToString();

            ss = string.Concat(ss, " ", icol.R.ToString(), " ", icol.G.ToString(), " ", icol.B.ToString());

            string s4 = textBox7.Text;

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    ss = "update testo set denom_testo = '" + s2 + "', font = '" + s3 + "', colore ='" + ss + "', altezza_tab = '" + s4 + 
                        "' WHERE codice_testo = '";
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");

                    cmd.CommandText = ss;

                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //  annulla

            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox4.BackColor = colorDialog1.Color;
            }
        }
        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);


        }

        private void button3_Click(object sender, EventArgs e)
        {
            //  font

            FontDialog fontDialog1 = new FontDialog();
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = fontDialog1.Font.ToString();
                textBox5.Text = fontDialog1.Font.Height.ToString();
                textBox6.Text = fontDialog1.Font.Size.ToString();
            }
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

    }
}
